package com.joesem.sale.client.logic.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.joesem.sale.client.utils.click
import com.jiaxing.sale.client.databinding.FragmentHomeBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.db.LoginTable
import com.joesem.sale.client.logic.event.EventListActivity
import com.joesem.sale.client.network.data.OrderRankingMode
import java.util.Calendar

class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    private val rankingAdapter =
        object : BaseQuickAdapter<OrderRankingMode, BaseViewHolder>(R.layout.item_order_ranking) {
            @SuppressLint("SetTextI18n", "UseCompatLoadingForDrawables")
            override fun convert(holder: BaseViewHolder, item: OrderRankingMode) {
                holder.setText(R.id.tvName, item.realname.orEmpty())
                holder.setText(R.id.tvCount, "完成" + item.count.toString())
                holder.getView<TextView>(R.id.ivRanking).apply {
                    when (holder.layoutPosition) {
                        0 -> {
                            text = ""
                            setCompoundDrawablesRelativeWithIntrinsicBounds(
                                context.resources.getDrawable(
                                    R.drawable.icon_homepages_first
                                ), null, null, null
                            )
                        }
                        1 -> {
                            setCompoundDrawablesRelativeWithIntrinsicBounds(
                                context.resources.getDrawable(
                                    R.drawable.icon_homepages_second
                                ), null, null, null
                            )
                            text = ""
                        }
                        2 -> {
                            setCompoundDrawablesRelativeWithIntrinsicBounds(
                                context.resources.getDrawable(
                                    R.drawable.icon_homepages_third
                                ), null, null, null
                            )
                            text = ""
                        }
                        else -> {
                            setCompoundDrawablesRelativeWithIntrinsicBounds(
                                null,
                                null,
                                null,
                                null
                            )
                            text = " " + (holder.layoutPosition + 1).toString()
                        }
                    }
                }
            }
        }
    private val viewModel by createViewModel<MainViewModel>()

    override fun FragmentHomeBinding.initView(savedInstanceState: Bundle?) {
        rvRankingContent.adapter = rankingAdapter
    }

    override fun FragmentHomeBinding.initListener() {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                viewModel.orderStatsNum()
                viewModel.orderRanking()
                viewModel.accountInfo()
            }

            override fun onCreate(owner: LifecycleOwner) {
                srlRefresh.isRefreshing = true
            }
        })
        srlRefresh.setOnRefreshListener {
            viewModel.orderStatsNum()
            viewModel.orderRanking()
            viewModel.accountInfo()
        }
        btnWorkOrder.click {
            (requireActivity() as MainActivity).bind.bottomMenu.selectedItemId =
                R.id.menu_work_order
        }
        btnEvent.click {
            launch<EventListActivity>()
        }
        btnKnowledge.click {

        }
        btnSpare.click {

        }
        btnCustomer.click {

        }
    }

    override fun FragmentHomeBinding.initIntent(intent: Any?) {
        when (intent) {
            is MainViewModel.OrderStatusNumCountIntent -> {
                srlRefresh.isRefreshing = false
                tvNoComplete.text = intent.num.未完成.toString()
                tvWaitAssignment.text = intent.num.已创建.toString()
                tvDispatchOrder.text = intent.num.已指派.toString()
                tvAccept.text = intent.num.已接受.toString()
                tvInProgress.text = intent.num.未完成.toString()
            }
            is MainViewModel.OrderRankingCountIntent -> {
                srlRefresh.isRefreshing = false
                tvRanking.visible()
                cdRankingContent.visible()
                rankingAdapter.setList(intent.data)
            }
            is MainViewModel.AccountIntent -> {
                srlRefresh.isRefreshing = false
                val user = intent.data.realname
                val hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
                tvLoginName.text = when (hour) {
                    in 4..11 -> "上午好，$user"
                    in 12..13 -> "中午好，$user"
                    in 14..18 -> "下午好，$user"
                    in 19..23 -> "晚上好，$user"
                    else -> "晚上好，$user"
                }
            }
        }
    }
}